<?php


namespace app\admin\controller;
use app\admin\model\Bclass as BclassModel;
use app\admin\model\Sclass as SclassModel;
use app\admin\model\Foods as FoodsModel;
use think\Controller;
class Sclass extends Controller
{

    //调用添加二级分类页面
    function addsclass(){
        $model=new BclassModel();
        $bdata = $model->getInfo();
        $this->assign('bdata',$bdata);
        return $this->fetch();
    }

    //添加二级分类
    function doadd(){
        $data=$this->request->param();
        $model=new SclassModel();
        $res=$model->save($data);
        if($res){
            return alert_success('添加成功','Sclass/listsclass');
        }else{
            return alert_error('添加分类失败，请检测输入!');
        }

    }

    //查询二级分类信息
    function listsclass(){
        $model=new SclassModel();
        $sdata =$model->getList();
$model1=new BclassModel();
        foreach($sdata as &$row){
            $pid = $row['pid'];
            $bdata = $model1->getName($pid);
            $row['bname'] = $bdata['bname'];
        }$this->assign('bdata',$bdata);
        $this->assign('sdata',$sdata);
      return  $this->fetch();
    }
    public function edit($no){
        $model=new SclassModel();
        $sinfo=$model->getSinfo($no);
        $modelb=new BclassModel();
        $bdata = $modelb->getInfo();
        $this->assign('bdata',$bdata);
        $this->assign('sinfo',$sinfo);
        return $this->fetch('edit');
    }

    public function doedit(){
        $data=$this->request->param();
        $model=new SclassModel();
        $res=$model->updateSclass($data);
        if ($res) {
            return alert_success('修改成功','Sclass/listsclass');
        } else {
            return alert_error('修改失败','sclass/edit');
        }
    }
    //删除二级分类
    function del($no)
    {
        $model1 = new FoodsModel();
        $res = $model1->findFood($no);
        if (!empty($res)) {
            $this->error('该分类下存在商品,不允许删除该分类!');
            exit;
        } else {
            $model = new SclassModel();
            if ($model->delSclass($no)) {
                return alert_success('删除成功', 'sclass/listsclass');
            } else {
                return alert_error('删除分类失败', 'edit');
            }
        }
    }
}